package cn.st4rlight.util.excel.converter;

import java.util.Objects;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import cn.st4rlight.util.enums.HasCodeAndDesc;

/**
 * @author st4rlight <st4rlight@163.com>
 * Created on 2024-07-30
 */
public class EnumDescConverter implements Converter<HasCodeAndDesc> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return HasCodeAndDesc.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<HasCodeAndDesc> context) throws Exception {
        HasCodeAndDesc hasCodeAndDesc = context.getValue();
        if (Objects.isNull(hasCodeAndDesc)) {
            return new WriteCellData<>(CellDataTypeEnum.STRING, "-");
        }

        return new WriteCellData<>(CellDataTypeEnum.STRING, hasCodeAndDesc.getDesc());
    }
}
